package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbActivity;
import com.ruoyi.system.mapper.TbOrganizerMapper;
import com.ruoyi.system.domain.TbOrganizer;
import com.ruoyi.system.service.ITbOrganizerService;

/**
 * 组织者管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbOrganizerServiceImpl implements ITbOrganizerService 
{
    @Autowired
    private TbOrganizerMapper tbOrganizerMapper;

    /**
     * 查询组织者管理
     * 
     * @param id 组织者管理主键
     * @return 组织者管理
     */
    @Override
    public TbOrganizer selectTbOrganizerById(Long id)
    {
        return tbOrganizerMapper.selectTbOrganizerById(id);
    }

    /**
     * 查询组织者管理列表
     * 
     * @param tbOrganizer 组织者管理
     * @return 组织者管理
     */
    @Override
    public List<TbOrganizer> selectTbOrganizerList(TbOrganizer tbOrganizer)
    {
        return tbOrganizerMapper.selectTbOrganizerList(tbOrganizer);
    }

    /**
     * 新增组织者管理
     * 
     * @param tbOrganizer 组织者管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbOrganizer(TbOrganizer tbOrganizer)
    {
        tbOrganizer.setCreateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbOrganizer.getImages() != null && tbOrganizer.getImages().contains(",")) {
            tbOrganizer.setImages(tbOrganizer.getImages().split(",")[0]);
        }
        int rows = tbOrganizerMapper.insertTbOrganizer(tbOrganizer);
        insertTbActivity(tbOrganizer);
        return rows;
    }

    /**
     * 修改组织者管理
     * 
     * @param tbOrganizer 组织者管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbOrganizer(TbOrganizer tbOrganizer)
    {
        tbOrganizer.setUpdateTime(DateUtils.getNowDate());
        // 只保留images字段的第一个url
        if (tbOrganizer.getImages() != null && tbOrganizer.getImages().contains(",")) {
            tbOrganizer.setImages(tbOrganizer.getImages().split(",")[0]);
        }
        tbOrganizerMapper.deleteTbActivityByOrganizerId(tbOrganizer.getId());
        insertTbActivity(tbOrganizer);
        return tbOrganizerMapper.updateTbOrganizer(tbOrganizer);
    }

    /**
     * 批量删除组织者管理
     * 
     * @param ids 需要删除的组织者管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOrganizerByIds(Long[] ids)
    {
        tbOrganizerMapper.deleteTbActivityByOrganizerIds(ids);
        return tbOrganizerMapper.deleteTbOrganizerByIds(ids);
    }

    /**
     * 删除组织者管理信息
     * 
     * @param id 组织者管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbOrganizerById(Long id)
    {
        tbOrganizerMapper.deleteTbActivityByOrganizerId(id);
        return tbOrganizerMapper.deleteTbOrganizerById(id);
    }

    /**
     * 新增亲社会行为信息
     * 
     * @param tbOrganizer 组织者管理对象
     */
    public void insertTbActivity(TbOrganizer tbOrganizer)
    {
        List<TbActivity> tbActivityList = tbOrganizer.getTbActivityList();
        Long id = tbOrganizer.getId();
        if (StringUtils.isNotNull(tbActivityList))
        {
            List<TbActivity> list = new ArrayList<TbActivity>();
            for (TbActivity tbActivity : tbActivityList)
            {
                tbActivity.setOrganizerId(id);
                list.add(tbActivity);
            }
            if (list.size() > 0)
            {
                tbOrganizerMapper.batchTbActivity(list);
            }
        }
    }
}
